package com.pdg.islandinterloper.database.tableproviders;

import java.util.ArrayList;
import java.util.List;

import com.pdg.islandinterloper.MainActivity;
import com.pdg.islandinterloper.database.IslandInterloperDatabase;
import com.pdg.islandinterloper.database.baseclasses.TableProviderBase;
import com.pdg.islandinterloper.database.objects.Profile;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ProfileTableProvider extends TableProviderBase<MainActivity,IslandInterloperDatabase>{
	
	private static final String TABLE_NAME = "profiles";
	private static final String FIELD_ID = "_id";
	private static final String FIELD_NAME = "profile_name";
	private static final String FIELD_STATE = "profile_state";
	private static final String FIELD_WORLD_WIDTH = "world_width";
	private static final String FIELD_WORLD_HEIGHT = "world_height";
	private static final String FIELD_WORLD_WRAP_X = "world_wrap_x";
	private static final String FIELD_WORLD_WRAP_Y = "world_wrap_y";
	private static final String FIELD_ISLAND_VIEW = "island_view";
	private static final String FIELD_MAXIMUM_TURNS = "maximum_turns";
	private static final String FIELD_TURN_INTERVAL = "turn_interval";
	private static final String FIELD_ISLAND_COUNT = "island_count";
	private static final String FIELD_ISLAND_SPACING = "island_spacing";
	private static final String TABLE_CREATE = "CREATE TABLE IF NOT EXISTS `"+
			TABLE_NAME+"` (`"+
			FIELD_ID+"` INTEGER PRIMARY KEY AUTOINCREMENT, `"+
			FIELD_NAME+"` TEXT NOT NULL UNIQUE, `"+
			FIELD_STATE+"` TEXT NOT NULL DEFAULT '"+Profile.DEFAULT_PROFILE_STATE.toString()+"',`"+
			FIELD_WORLD_WIDTH+"` INTEGER NOT NULL DEFAULT "+Profile.getDefaultWorldWidth()+",`"+
			FIELD_WORLD_HEIGHT+"` INTEGER NOT NULL DEFAULT "+Profile.getDefaultWorldHeight()+",`"+
			FIELD_WORLD_WRAP_X+"` TEXT NOT NULL DEFAULT "+Profile.getDefaultWorldWrapX()+",`"+
			FIELD_WORLD_WRAP_Y+"` TEXT NOT NULL DEFAULT "+Profile.getDefaultWorldWrapY()+",`"+
			FIELD_ISLAND_VIEW+"` INTEGER NOT NULL DEFAULT "+Profile.getDefaultIslandView()+",`"+
			FIELD_MAXIMUM_TURNS+"` INTEGER NOT NULL DEFAULT "+Profile.getDefaultMaximumTurns()+",`"+
			FIELD_TURN_INTERVAL+"` INTEGER NOT NULL DEFAULT "+Profile.getDefaultTurnInterval()+",`"+
			FIELD_ISLAND_COUNT+"` INTEGER NOT NULL DEFAULT "+Profile.getDefaultIslandCount()+",`"+
			FIELD_ISLAND_SPACING+"` INTEGER NOT NULL DEFAULT "+Profile.getDefaultIslandSpacing()+");";
	private static final String TABLE_DELETE = "DROP TABLE IF EXISTS `"+TABLE_NAME+"`;";
	private static final String HAS_PROFILES_QUERY = "SELECT COUNT(`"+FIELD_ID+"`) FROM `"+TABLE_NAME+"`;";
	private static final String HAS_PROFILE_BY_ID_QUERY = "SELECT COUNT(`"+FIELD_ID+"`) FROM `"+TABLE_NAME+"` WHERE `"+FIELD_ID+"`=?;";
	private static final String HAS_PROFILE_BY_NAME_QUERY = "SELECT COUNT(`"+FIELD_NAME+"`) FROM `"+TABLE_NAME+"` WHERE `"+FIELD_NAME+"`=?;";
	private static final String GET_PROFILE_BY_ID_QUERY = "SELECT `"+
			FIELD_ID+"`,`"+
			FIELD_NAME+"`,`"+
			FIELD_STATE+"`,`"+
			FIELD_WORLD_WIDTH+"`,`"+
			FIELD_WORLD_HEIGHT+"`,`"+
			FIELD_WORLD_WRAP_X+"`,`"+
			FIELD_WORLD_WRAP_Y+"`,`"+
			FIELD_ISLAND_VIEW+"`,`"+
			FIELD_MAXIMUM_TURNS+"`,`"+
			FIELD_TURN_INTERVAL+"`,`"+
			FIELD_ISLAND_COUNT+"`,`"+
			FIELD_ISLAND_SPACING+"` FROM `"+
			TABLE_NAME+"` WHERE `"+
			FIELD_ID+"`=?";
	private static final String DELETE_WHERE_CLAUSE = "`"+FIELD_ID+"`=?";
	private static final String UPDATE_WHERE_CLAUSE = "`"+FIELD_ID+"`=?";
	private static final int ZERO = 0;//TODO
	private static final int ONE = 1;//TODO

	public ProfileTableProvider(IslandInterloperDatabase theParent) {
		super(theParent);
	}

	@Override
	public void onCreate(SQLiteDatabase theDatabase) {
		theDatabase.execSQL(TABLE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase theDatabase, int theOldVersion,
			int theNewVersion) {
		theDatabase.execSQL(TABLE_DELETE);
		onCreate(theDatabase);
	}
	
	public boolean hasProfileById(Long theId){
		Cursor theCursor = getParent().getWritableDatabase().rawQuery(HAS_PROFILE_BY_ID_QUERY,new String[]{theId.toString()});
		theCursor.moveToFirst();
		boolean theResult = theCursor.getLong(ZERO)>ZERO;
		theCursor.close();
		return theResult;
	}
	
	public boolean hasProfileByName(String theName){
		Cursor theCursor = getParent().getWritableDatabase().rawQuery(HAS_PROFILE_BY_NAME_QUERY,new String[]{theName});
		theCursor.moveToFirst();
		boolean theResult = theCursor.getLong(ZERO)>ZERO;
		theCursor.close();
		return theResult;
	}
	
	public boolean hasProfiles(){
		Cursor theCursor = getParent().getWritableDatabase().rawQuery(HAS_PROFILES_QUERY, null);
		theCursor.moveToFirst();
		boolean theResult = theCursor.getLong(ZERO)>ZERO;
		theCursor.close();
		return theResult;
	}
	
	public Profile getProfileById(Long theId){
		Cursor theCursor = getParent().getWritableDatabase().rawQuery(GET_PROFILE_BY_ID_QUERY,new String[]{theId.toString()});
		if(!theCursor.moveToFirst()) return null;
		Profile theProfile = new Profile(theCursor);
		theCursor.close();
		return theProfile;
	}
	
	public boolean deleteProfileById(Long theId){
		return getParent().getWritableDatabase().delete(TABLE_NAME, DELETE_WHERE_CLAUSE, new String[]{theId.toString()}) > 0;
	}
	
	public Profile createProfile(Profile theProfile){
		if(hasProfileByName(theProfile.getProfileName())) return null;
		ContentValues values = getProfileContentValues(theProfile);
		long theId = getParent().getWritableDatabase().insert(TABLE_NAME, null, values);
		//TODO cascading creates
		return getProfileById(theId);
	}
	public ContentValues getProfileContentValues(Profile theProfile) {
		ContentValues values=new ContentValues();
		values.put(FIELD_NAME, theProfile.getProfileName());
		values.put(FIELD_ISLAND_VIEW, theProfile.getIslandView());
		values.put(FIELD_MAXIMUM_TURNS, theProfile.getMaximumTurns());
		values.put(FIELD_STATE, theProfile.getProfileState().toString());
		values.put(FIELD_TURN_INTERVAL, theProfile.getTurnInterval());
		values.put(FIELD_WORLD_HEIGHT, theProfile.getWorldHeight());
		values.put(FIELD_WORLD_WIDTH, theProfile.getWorldWidth());
		values.put(FIELD_WORLD_WRAP_X, theProfile.getWorldWrapX().toString());
		values.put(FIELD_WORLD_WRAP_Y, theProfile.getWorldWrapY().toString());
		values.put(FIELD_ISLAND_COUNT, theProfile.getIslandCount().toString());
		values.put(FIELD_ISLAND_SPACING, theProfile.getIslandSpacing().toString());
		return values;
	}
	public boolean updateProfile(Profile theProfile){
		ContentValues values = getProfileContentValues(theProfile);
		return getParent().getWritableDatabase().update(TABLE_NAME, values, UPDATE_WHERE_CLAUSE, new String[]{theProfile.getId().toString()})==ONE;
	}
	
	public List<Profile> getProfiles(){
		List<Profile> theProfiles = new ArrayList<Profile>();
		Cursor theCursor = getParent().getWritableDatabase().query(TABLE_NAME, new String[]{FIELD_ID,FIELD_NAME}, null, null, null, null, FIELD_NAME);
		if(theCursor.moveToFirst()){
			do{
				theProfiles.add(new Profile(theCursor));
			}while(theCursor.moveToNext());
		}
		theCursor.close();
		return theProfiles;
	}
}
